<?php
namespace Home\Controller;
use Think\Controller;
use Think\Model;

class ProductController extends Controller {
    //
    public function ProductList(){
        if(isset($_GET['label'])){
            $goodsres=$this->get_data($_GET['label'] );
            $title=$_GET['chinese'];
        }else if(isset($_GET['good_name'])){
            $good_name=trim(I('good_name'));
            $goodsres=$this->search_data($good_name);
            $title="全部商品";
        }else{
            $goodsres=$this->get_data("");
            $title="全部商品"; 
        }
        $labelTable=D('config');
        $where['name']='LABEL_SET';

        $labellist=$labelTable->where($where)->getfield('value');
        $str= explode("\n", $labellist);
        $arr=array();
        foreach ($str as $value){
           $arr2=explode(":", $value);
                   if($arr2[0]==""||$arr2[0]==" "||$arr2[0]==null){  
                   }else{
                   $arr[$arr2[0]]=$arr2[1];
                   }
        }
        $this->title=$title;
        $this->good_name=$good_name;
        $this->goodlist=$goodsres;
        $this->label=$arr;
        $this->display();
    }
    function get_data($keyword){
        $tablegoods=D('product');
        $sql = "select a.*,(CASE WHEN b.price1 is null then 0 else b.price1 end ) as price1,(CASE WHEN b.price1 is null then 0 else b.price2 end ) as price2 ,(CASE WHEN b.price1 is null then 0 else b.num1 end ) as num1,(CASE WHEN b.price1 is null then 0 else b.num2 end ) as num2 from gr_product a LEFT JOIN gr_activity_more_buy b ON (a.id=b.id)  where a.status=1 ";
        if($keyword==""||$keyword==null||$keyword==" "){
//            $sql="select a.*,(CASE WHEN (least(a.price,b.price1)) is null then a.price else least(a.price,b.price1) end ) as minprice  from gr_product a LEFT JOIN gr_activity_more_buy b ON (a.id=b.id)  where a.status=1 order by a.recommend desc,a.inventory desc,a.lastupdate desc";
//            $sql="select a.*,a.price as minprice  from gr_product a   where a.status=1 order by a.recommend desc,a.inventory desc,a.lastupdate desc";
            $sql .= " order by a.recommend desc,a.lastupdate desc,a.inventory desc";

        }else{
//            $sql="select a.*,(CASE WHEN (least(a.price,b.price1)) is null then a.price else least(a.price,b.price1) end ) as minprice  from gr_product a LEFT JOIN gr_activity_more_buy b ON (a.id=b.id)  where a.status=1 AND a.inventory >0 AND a.tagarray like '%".$keyword."%' order by a.recommend desc,a.inventory desc,a.createdate desc";
//            $sql="select a.*,a.price as minprice  from gr_product a  where a.status=1 AND a.inventory >0 AND a.tagarray like '%".$keyword."%' order by a.recommend desc,a.inventory desc,a.createdate desc";
            $sql .= " AND a.inventory >0 AND a.tagarray like '%".$keyword."%' order by a.recommend desc,a.inventory desc,a.createdate desc";
        }
        $model=new Model();
        $goodsres=$model->query($sql);
        $i=0;
        foreach($goodsres as $value){
               preg_match_all('/\["(.+?)\"(.+?)/', $value["picurlarray"], $arr4);
                 $goodsres[$i]['imgurl']=$arr4[1][0];
                 $data['pid']=$goodsres[$i]['id'];
                 $data['uid']=session('userid');
                 $flagres=D('cart')->where($data)->select();
                    if(count($flagres)==0){
                        $iscartflag=false;
                    }else{
                        $iscartflag=true;
                    }
                 $goodsres[$i]['iscart']=$iscartflag;
                 $i++;
            }
             return $goodsres;
        }
    function search_data($keyword){
        $sql = "select a.*,(CASE WHEN b.price1 is null then 0 else b.price1 end ) as price1,(CASE WHEN b.price1 is null then 0 else b.price2 end ) as price2 ,(CASE WHEN b.price1 is null then 0 else b.num1 end ) as num1,(CASE WHEN b.price1 is null then 0 else b.num2 end ) as num2 from gr_product a LEFT JOIN gr_activity_more_buy b ON (a.id=b.id)  where a.status=1 ";

        if($keyword==""||$keyword==null||$keyword==" "){
//            $sql="select a.*,(CASE WHEN (least(a.price,b.price1)) is null then a.price else least(a.price,b.price1) end) as minprice  from gr_product a LEFT JOIN gr_activity_more_buy b ON (a.id=b.id) where a.status=1 order by a.recommend desc,a.inventory desc,a.createdate desc ";
            $sql .= " order by a.recommend desc,a.inventory desc,a.createdate desc ";
        }else{
//            $sql="select a.*,(CASE WHEN (least(a.price,b.price1)) is null then a.price else least(a.price,b.price1) end) as minprice  from gr_product a LEFT JOIN gr_activity_more_buy b ON (a.id=b.id)  where a.status=1 AND a.name like '%".$keyword."%' order by a.recommend desc,a.inventory desc,a.createdate desc ";
            $sql.=" AND a.name like '%".$keyword."%' order by a.recommend desc,a.inventory desc,a.createdate desc ";
        }
            $model=new Model();
            $goodsres=$model->query($sql);
            $i=0;  
            foreach($goodsres as $value){
                 preg_match_all('/\["(.+?)\"(.+?)/', $value["picurlarray"], $arr4);
                 $goodsres[$i]['imgurl']=$arr4[1][0];
                 $data['pid']=$goodsres[$i]['id'];
                 $data['uid']=session('userid');
                 $flagres=D('cart')->where($data)->select();
                    if(count($flagres)==0){
                        $iscartflag=false;
                    }else{
                        $iscartflag=true;
                    }
                 $goodsres[$i]['iscart']=$iscartflag;
                 $i++;
            }
            return $goodsres;
       }
    public function ProductDetails(){
        $goodsId=$_GET['pid'];
        $data['pid']=$goodsId;
        $data['uid']=session('userid');
        $detailres=D('product')->query("select * from gr_product where id=".$goodsId."");
        $flagres=D('cart')->where($data)->select();
         if(count($flagres)==0){
             $detailres[0]["iscart"]=false;
         }else{
             $detailres[0]["iscart"]=true;
         }
        $map['id']=$goodsId;
        $activityMoreBuy=M('activity_more_buy');
        $resAct=$activityMoreBuy->where($map)->select();
        if(count($resAct)>0){
            $detailres[0]["price1"]="￥".$resAct[0]["price1"];
            if($resAct[0]["price2"] != 0){
                $detailres[0]["price2"]="￥".$resAct[0]["price2"];
            }else{
                $detailres[0]["price2"] = "";
            }
            if($resAct[0]["num2"] != 0){
                $detailres[0]["num2"]="≥".$resAct[0]["num2"]."件";
            }else{
                $detailres[0]["num2"] = "";
            }
            $detailres[0]["num1"]="≥".$resAct[0]["num1"]."件";
            $detailres[0]["num0"]="1-".($resAct[0]["num1"]-1)."件";
        }else{
            $detailres[0]["num0"]="";
            $detailres[0]["price1"]="";
            $detailres[0]["num1"]="";
        }
        preg_match_all('/\"(.+?)\"(.+?)/', $detailres[0]["picurlarray"], $arr4);
        if(strpos($detailres[0]['tagarray'],"SINGLE")===false){
            $detailres[0]['independent']=2;
        }else{
            $detailres[0]['independent']=1;
        };
         $detailres[0]["imgurl"]=$arr4[1];
         $this->imgarr=$detailres[0]["imgurl"];
         $this->goodlist=$detailres;
         $this->display();
    }
    public function strsplit($str){
    }
    public function Productajax(){
        $pid=$_POST['pid'];
        $act=$_POST['actStatus'];
        $data['pid']=$pid;
        $data['uid']=session('userid');
        $cartTable= D('cart');
        if($act=="add"){
             $flagres=D('cart')->where($data)->select();
                if(count($flagres)==0){
                    $cartTable->add($data);
                }
           
        }else{
           $cartTable->where($data)->delete();
        }

    }
}